package com.ahp.action;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.ahp.bean.EvaleObject;
import com.ahp.bean.Factor;
import com.ahp.bean.Module;
import com.ahp.bean.User;
import com.ahp.service.EvaleObjectService;
import com.ahp.service.ModuleService;
import com.ahp.util.PageList;
import com.ahp.util.Pages;

public class ModuleAction extends CommonActionSupport {
	private static final long serialVersionUID = 1L;
	private Module module;
	private ModuleService moduleService;
	private EvaleObjectService evaleObjectService;
	public Module getModule() {
		return module;
	}
	public void setModule(Module module) {
		this.module = module;
	}
	public ModuleService getModuleService() {
		return moduleService;
	}
	public void setModuleService(ModuleService moduleService) {
		this.moduleService = moduleService;
	}
	public EvaleObjectService getEvaleObjectService() {
		return evaleObjectService;
	}
	public void setEvaleObjectService(EvaleObjectService evaleObjectService) {
		this.evaleObjectService = evaleObjectService;
	}
	/**
	 * @return
	 * @date:Sep 7, 2009
	 * @author: 李晓娜
	 */
	public String searchDefaultAndMineModule(){
		User user = (User)getSession().get(User.USER_SESSION);
		List<Module> list = moduleService.searchDefaultAndMineModule(user);
		Map<String,Map<Integer,Set<Factor>>> modMap = moduleService.parseModule(list);
		getRequest().setAttribute("modules", list);
		getRequest().setAttribute("modMap", modMap);
		return SUCCESS;
	}
	
	/**
	 * @return
	 */
	public String searchUserModule(){
		User user = (User)getRequest().getSession().getAttribute(User.USER_SESSION);
		Pages pages=new Pages("searchUserModule.action",getRequest());
		PageList pagelist= moduleService.searchUserModule(user,pages);
		  getRequest().setAttribute("list",pagelist.getObjectList());
		  getRequest().setAttribute("pageStr",pagelist.getPageStr());
		return SUCCESS;
	}
	
	/**
	 * @return
	 * @date:Sep 8, 2009
	 * @author: 李晓娜
	 */
	public String enterLevelOneWeight(){
		List<Factor> list = moduleService.parseModuleGetOneLevel(module.getModId(),1);
		getRequest().setAttribute("levOneList", list);
		
		getSession().put(Module.SESSION_MODULE, module.getModId());
		
		return SUCCESS;
	}
	/**
	 * @return
	 * @date:Sep 8, 2009
	 * @author: 李晓娜
	 */
	public String levelOne() {
		Map<String,String[]> map = getRequest().getParameterMap();
		getRequest().getSession().setAttribute("levelOneResult", map);
		
		List<String[]> list = moduleService.parseLevelOne(map);
		
		getSession().put(Module.SESSION_LEVEL_ONE_VALUE, list);
		
		Integer modId = (Integer)getSession().get(Module.SESSION_MODULE);
		
		List<Factor> listOne = moduleService.parseModuleGetOneLevel(modId,1);
		List<Factor> listTwo = moduleService.parseModuleGetOneLevel(modId,2);
		Map<Integer,List<Factor>> mapOneTwo = moduleService.parseOneTwoLevelFactor(listOne, listTwo);

		getRequest().setAttribute("levOneList", listOne);
		getRequest().setAttribute("oneTwoMap", mapOneTwo);
		return SUCCESS;
	}
	
	/**
	 * @return
	 * @date:Sep 8, 2009
	 * @author: 李晓娜
	 */
	public String levelTwo() {
		Map<String,String[]> map = getRequest().getParameterMap();
		getRequest().getSession().setAttribute("levelTwoResult", map);
		List<String[]> levelOneList = null;
		if(getSession().get(Module.SESSION_LEVEL_ONE_VALUE)!=null){
			levelOneList = (List<String[]>)getSession().get(Module.SESSION_LEVEL_ONE_VALUE);
		}
		Map<String,Float> multMap = moduleService.parseLevelTwo(map,levelOneList);
		getSession().put(Module.SESSION_LEVEL_TOW_MULTI, multMap);
		
		/////////////////////////////
		
		List<EvaleObject> list = evaleObjectService.findEvaleObjects((List<Integer>)getSession().get(Module.SESSION_OBJECT) ,(Integer)getSession().get(Module.SESSION_TASK));
		getRequest().setAttribute("objectList", list);
		
		Integer modId = (Integer)getSession().get(Module.SESSION_MODULE);
		
		List<Factor> listTwo = moduleService.parseModuleGetOneLevel(modId,2);
		getRequest().setAttribute("levTwoList", listTwo);
		return SUCCESS;
	}
	
	/**
	 * @return
	 * @date:Sep 8, 2009
	 * @author: 李晓娜
	 */
	public String ahp(){
		Map<String,String[]> map = getRequest().getParameterMap();
//		for (Iterator<String> iterator = map.keySet().iterator(); iterator.hasNext();) {
//			String key = iterator.next();
//			String value = map.get(key)[0];
//			System.out.println(key+"rs:::::"+value);
//		}
		getRequest().getSession().setAttribute("ahpResult", map);
		Map<String,Set<String[]>> list = moduleService.parseLevelTwo(map);
		
		Map<String,Float> multMap = (Map<String,Float>)getSession().get(Module.SESSION_LEVEL_TOW_MULTI);
		Map<String, Float> resultMap = moduleService.ahp(list, multMap);
		evaleObjectService.saveObjectScore(resultMap);
		resultMap = evaleObjectService.objectIdToName(resultMap);
		
		getRequest().setAttribute("resultMap", resultMap);
		return SUCCESS;
	}
	
	public String addModule(){
		moduleService.addModule(module);
		getRequest().setAttribute("module",module);
		return SUCCESS;
	}
}
